package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.rock.YarnWhiteEntity;
import com.hllxd.hism.star.basic.service.service.UserService;
import com.hllxd.hism.star.basic.service.service.YarnWhiteService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 计算任务白名单管理
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/aaa")
public class AaaController2 {

    @Autowired
    private YarnWhiteService yarnWhiteService;

    @Autowired
    private UserService userService;

    @Data
    public static class Param {
        public Long id;
        public String ids;
        private String phoneNumber;
        /** 客户端时间戳 */
        public Long ts;
        /** 版本*/
        public String version;
        /** 渠道 */
        public String channel;
        /** 手机号 */
        public String mobile;
    }

    @RestGrpcMapping("/listPage.do")
    public NTSTATUS listPage(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
//            List<YarnWhiteEntity> items = yarnWhiteService.listYarnWhite();
//            log.info("items: {}.", items.size());
            result.setData(null);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    public static class UserInfo {
        public String userID;
        public String openid;
        public String mobile;

        public static UserInfo build(String userID,String mobile, String openid) {
            UserInfo thiz = new UserInfo();
            thiz.userID = userID;
            thiz.mobile = mobile;
            thiz.openid = openid;
            return thiz;
        }
    }
    @RestGrpcMapping("/ping.do")
         public NTSTATUS ping(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            UserInfo userInfo = UserInfo.build(params.userID, "13811834706", "wxid13811834706");
            result.setData(userInfo);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/pingDetail.do")
    public NTSTATUS pingDetail(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            if(params.userID==null) {
                throw new BusinessException("001","param is invalid.");
            }
            result.setData(userService.pingDetail(params.userID));
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    @RestGrpcMapping("/setYarnWhite.do")
    public NTSTATUS setYarnWhite(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            YarnWhiteEntity aa = params.driverObject(YarnWhiteEntity.class);
            Object res = yarnWhiteService.setYarnWhite(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/deleteYarnWhite.do")
    public NTSTATUS deleteYarnWhite(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            Boolean ret = yarnWhiteService.deleteYarnWhite(aa.ids);
            result.setData(ret);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
}
